package org.gcoat;

import java.io.File;

import org.gcoat.analysis.GCLogHandler;
import org.gcoat.analysis.JavaGCLogParser;
import org.gcoat.list.IntList;
import org.gcoat.list.LongList;
import org.gcoat.sequence.DefaultGCTimeSequence;

public class Test {
	public static void main(String[] args) throws Exception {
		File file = new File("gc-sample.log");
		JavaGCLogParser parser = new JavaGCLogParser();
		DefaultGCTimeSequence seq = new DefaultGCTimeSequence();
		int estimatedNumberOfEntries = parser.estimateNumberOfEntries(file) / 3;
		System.out.printf("estimatedNumberOfEntries = %d\n", estimatedNumberOfEntries);
		parser.parse(file, seq);
		for (int i = 0, ii = seq.size(); i < ii; ++i) {
			System.out.printf("%3d %5d %15d %15d %15d %s\n", i, seq.getLineNumber(i), seq.getTime(i), seq.getAccumulatedTime(i), seq.getTotalAccumulatedTime(i), seq.getMode(i) == GCLogHandler.RUNNING ? "RUNNING" : "STOPPED");
		}
	}

	static class Handler implements GCLogHandler {
		private final LongList times_;
		private final IntList lines_;

		public Handler(LongList times, IntList lines) {
			times_ = times;
			lines_ = lines;
		}

		public void append(int mode, long durationInNanos, int lineNumber) {
			assert (times_.size() & 1) == mode : "mode=" + mode + " size=" + times_.size();
			times_.add(durationInNanos + times_.get(times_.size() - 2));
			lines_.add(lineNumber);
		}
	}
}
